package com.dlc.felear.xiaoensale.helper;


import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.daimajia.numberprogressbar.NumberProgressBar;
import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.customView.LoadingDialog;
import com.dlc.felear.xiaoensale.utils.NetUtil;
import com.dlc.felear.xiaoensale.utils.UpLoadAPK;
import com.itdlc.android.library.utils.NetStateUtils;

public class UpdateAppHelper implements UpLoadAPK.DownloadStatusCallback{
    private static final String TAG="UpdateAppHelper";
    private Context context;
    private NumberProgressBar bnp;
    private Dialog progressBarDialog;
    private UpLoadAPK upLoadAPK;
    private LoadingDialog loadingDialog;

    public UpdateAppHelper(Context context) {
        this.context = context;
        upLoadAPK=new UpLoadAPK(context);
        upLoadAPK.setCallback(this);
        loadingDialog=new LoadingDialog(context,"正在安装");
    }

    public void uploadApk() {
        if(!NetStateUtils.isNetworkConnected(context)){
            apkVersionMsg("离线状态,请连接网络");
            return;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(R.drawable.app_logo);
        builder.setTitle("版本更新");
        builder.setMessage("版本更新");
        builder.setCancelable(true);
        builder.setPositiveButton("检查更新", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                upLoadAPK.upLoadApkInfo(false);
            }
        });

        builder.show();

    }

    public void setProgressDialog(float percent) {
        try {
            if (bnp != null) {
                bnp.setProgress((int) (percent* 100));
            }
        }catch (Exception e){

        }

    }


    /**
     * 下载进度条
     */
    public void showUpdateProgressDialog() {
        View view = LayoutInflater.from(context).inflate(R.layout.number_progress, null);
        bnp = view.findViewById(R.id.progress);
        bnp.setMax(100);
        bnp.setProgressTextColor(Color.BLUE);
        bnp.setProgress(0);
        bnp.setProgressTextSize(30);
        progressBarDialog = new Dialog(context);
        progressBarDialog.setContentView(view);
        progressBarDialog.setCanceledOnTouchOutside(false);
        progressBarDialog.show();
    }

    public void closeProgressBarDialog(){
        progressBarDialog.dismiss();
    }


    @Override
    public void downloadStatusCallback(float progress, long total, int id) {
        if(bnp==null){
            showUpdateProgressDialog();
        }
        Log.e(TAG,"progress="+progress+" total="+total);
        setProgressDialog(progress);
        if(progress==1.0f){
            closeProgressBarDialog();
            loadingDialog.show();
        }
    }

    @Override
    public void apkVersionMsg(String msg) {
        Log.e(TAG,"软件版本信息:"+msg);
        Toast.makeText(context,msg,Toast.LENGTH_LONG).show();
    }
}
